var map;
var track = false;
var trackerPoints = new Array(0);
var output_string;
var polyline, ePolyline;
var start_marker;
var globalUpdateValue;
var myZoomFactor, myNumLevels, verySmall;

function gotoAddress() {
	var address = document.getElementById("initialLocationForm").address.value;
	var geocoder = new GClientGeocoder();
	geocoder.getLocations(address, function(result) {
		if (result.Status.code == G_GEO_SUCCESS) {
			var place = result.Placemark[0];
			var zLevel = Math.min(3*place.AddressDetails.Accuracy,17);
			var center = new GLatLng(
				place.Point.coordinates[1],
				place.Point.coordinates[0]
			);
			map.setCenter(center, zLevel);
		} else {
			alert(address + " not found")
		}
	});
}


function start_track_command() {
	trackerPoints = new Array(0);
	if (polyline != undefined) {map.removeOverlay(polyline);};
	if (start_marker != undefined) {map.removeOverlay(start_marker);};
	document.getElementById("output_form").results.value = "";
	track = true;
}
function stop_track_command() {
	if(track) {
		track = false;
		map.removeOverlay(polyline);

		verySmall = 1*document.getElementById("output_form").verySmall.value;
		myZoomFactor = 1*document.getElementById("output_form").zoomFactor.value;
		myNumLevels = 1*document.getElementById("output_form").numLevels.value;
		var polylineEncoder = new PolylineEncoder(
		  myNumLevels,myZoomFactor,verySmall,true);
		var result = polylineEncoder.dpEncode(trackerPoints);
		console.log(result);
		var ePolyline = new GPolyline.fromEncoded({
		  color: "#0000ff",
		  weight: 4,
		  opacity: 0.8,
		  points: result.encodedPoints,
		  levels: result.encodedLevels,
		  zoomFactor: 2,
		  numLevels: 18
		});
		map.addOverlay(ePolyline);
 
		outString = "new GPolyline.fromEncoded({\n  color: \"#0000ff\",\n  weight: 4,\n  opacity: 0.8,\n  points: ";
		outString = outString + "\"" + result.encodedPointsLiteral + "\",\n";
		outString = outString + "  levels: \"" + result.encodedLevels + "\",\n";
		outString = outString + "  zoomFactor: " + myZoomFactor + ",\n";
		outString = outString + "  numLevels: " + myNumLevels + "\n});\n";
		document.getElementById("output_form").results.value = outString;
		document.getElementById("submitbutton").disabled=false;
	}
}

function clear_last_command() {
	if(track && trackerPoints.length == 1) {
		trackerPoints.pop();
		map.removeOverlay(start_marker);
	}
	if(track && trackerPoints.length == 2) {
		trackerPoints.pop();
		map.removeOverlay(polyline);
	}
	if(track && trackerPoints.length > 2) {
		trackerPoints.pop();
		map.removeOverlay(polyline);
		map.addOverlay(polyline = new GPolyline(trackerPoints, "#0000ff", 4, 0.8));
	}
}

function clear_all_command() {
	trackerPoints = new Array(0);
	map.clearOverlays();
	document.getElementById("output_form").results.value = "";
}

function mapClick(marker, point) {
	var displayedPoints;
	if (track) {
		map.panTo(point);
		trackerPoints.push(point);
		if(trackerPoints.length == 1) {
			map.addOverlay(start_marker = new GMarker(point, {title:"start", clickable:false}));
		}
		if(trackerPoints.length > 1) {
			if(trackerPoints.length > 2) {
				map.removeOverlay(polyline);
			}
			if(globalUpdateValue == 2) {
				displayedPoints = trackerPoints;
			}
			if(globalUpdateValue == 1) {
				displayedPoints = trackerPoints.slice(-5);
			}
			
			map.addOverlay(polyline = new GPolyline(displayedPoints, "#0000ff", 4, 0.8));
		}
	}
}

function setUpdate(updateValue) {
	globalUpdateValue = updateValue;
}

function load() {
	map = new GMap(document.getElementById("map"), 
		{draggableCursor: 'crosshair', draggingCursor: 'pointer'});
	GEvent.addListener(map, "click", mapClick);
	GEvent.addListener(map, "moveend", function() {
		var zoomLevel = map.getZoom();
		var center = map.getCenter();
		document.getElementById("message").innerHTML = 
			"zoom level = " + zoomLevel.toString() + "<br />\n" +
			"center = " + center.toString();
	});
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(52.1295, -0.2163), 14, G_HYBRID_MAP);

	output_string = "";
	//document.getElementById("initialLocationForm").address.value = "";
	document.getElementById("output_form").results.value = output_string;
	document.getElementById("output_form").updateRadio[0].checked = "true";
	document.getElementById("output_form").verySmall.value = 0.00001;
	document.getElementById("output_form").zoomFactor.value = 2;
	document.getElementById("output_form").numLevels.value = 18;
	setUpdate(2);
}
